<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||	
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws. 
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.). 
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations). 
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Groups/classes/PhpFox_ComponentEditCategories.class.php, $Author:  $ */



/** Component for edit group categories
 * @version $Id: PhpFox_ComponentDelete.class.php 
 *  Max Kovrigovich
 * @package module.groups
 */
class PhpFox_Mod_Groups_ComponentEditCategories extends BaseComponent
{
    /** Processing method
     */
    function process()
    {
        //check permissions
        $oSecurityService = App::getModuleService('Account', 'Security');
		/* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
		$oSecurityService->checkPerm('groups', array('full')); 
        
        $oGroupsService = $this->_oModule->getService('Groups');
        /* @var $oGroupsService PhpFox_Mod_Groups_ServiceGroups */
        
        $oReq = &App::getRequest();
        /* @var $oReq Request */
        $oUrl = &App::getUrl(); 
        $oTpl = $this->_oModule->getTemplate();

        $iGroupId = $oReq->getInt('id', 0);
        
        $bIsDeleted = false;
        $bIsInserted = false;                
            
        if($oReq->getAction() == 'delete' && ($bIsOwner OR App::isStaff()) )
        {
            $bIsDeleted = true;
        }
        $sAction = $oReq->getAction();
        $sName = $oReq->get('name', '');
        $iCategoryId = $oReq->getInt('id', 0);
        $sResult = '';
        if($oGroupsService->isUniqueCategory($sName, $iCategoryId))
        {
	        if($oReq->getAction() == 'create')
	        {
	            $sName = trim($oReq->get('name'));
                if($sName != '')
                {
                    //$sResult = App::format('Mod_Groups.msg_category_create', array($sName));
    	            $oGroupsService->editCategory(trim($sName));
                     $oUrl->clearParams('act');
                     $oUrl->clearParams('id');
                     App::forward($oUrl->getUrl(), App::format('Mod_Groups.msg_category_create'), 'message', App::getPageName(), 'gcategory'); 
                }
    	        else
        	        $this->addErrors(App::format('Mod_Groups.error_empty_name'));
	        }
	        elseif($oReq->getAction() == 'update')
	        {
	            $sName = trim($oReq->get('name'));
	            if($sName != '')
	            {
    	            $oGroupsService->editCategory(trim($sName), $iCategoryId);
    	            //$sResult = App::format('Mod_Groups.msg_category_update', array($sName));
                    $oUrl->clearParams('act');
                    $oUrl->clearParams('id');
                    App::forward($oUrl->getUrl(), App::format('Mod_Groups.msg_category_update'), 'message', App::getPageName(), 'gcategory'); 
        
	            }
                else
        	        $this->addErrors(App::format('Mod_Groups.error_empty_name'));    	            
	        }
        }
        else
	           $this->addErrors(App::format('Mod_Groups.error_name_exist', array($sName)));
        
        if($oReq->getAction() == 'delete')
        {
            $aCategory = $oGroupsService->getCategory($iCategoryId);
            $sResult = App::format('Mod_Groups.msg_category_delete', array($aCategory['name']));
            $oGroupsService->deleteCategory($iCategoryId);
            $oUrl->clearParams('act');
            $oUrl->clearParams('id');
            App::forward($oUrl->getUrl(), App::format('Mod_Groups.msg_category_delete'), 'message', App::getPageName(), 'gcategory'); 

        }
        
        $aList = $oGroupsService->getCategories();

        $oTpl->assign(array(
            'aErrors' => $this->getErrors(),
            'sName' =>$sName,
            'iCategoryId' => $iCategoryId,
            'sAction' => $sAction,
            'aList' => $aList,
        ));
        
        $sHtml = $oTpl->fetch('EditCategories.html');
        return $sHtml;
    }
}
?>